package com.admin.controller;

import com.admin.service.AdminMenuService;
import com.admin.util.CommonUtil;
import com.admin.util.LogUtil;
import com.admin.util.Result;
import com.data.entity.AdminMenu;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 菜单管理
 */
@RestController
@RequestMapping("/admin/menu")
@Api(tags = "菜单管理")
public class AdminMenuController {
    private Logger logger = LogUtil.getBussinessLogger();
    @Autowired
    private AdminMenuService adminMenuService;


    /**
     * 菜单列表
     * @return
     */
    @GetMapping("/listAll")
    @ApiOperation("菜单：列表")
    public Result listAll() {
        Result result = new Result();
        try {
            result.setCode(Result.Code.SUCCESS);
            result.setData(adminMenuService.listAll());
            result.setCount(adminMenuService.count());
        } catch (Exception e) {
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }

    /**
     * 添加菜单信息
     * @param name 菜单名称
     * @param url   菜单跳转地址
     * @param parentId  父级菜单id
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("菜单：添加")
    public Result add(String name, String url, Integer parentId) {
        Result result = new Result();
        try {
            AdminMenu adminMenu = new AdminMenu();
            if (!CommonUtil.isEmpty(parentId)) {
                adminMenu.setParentId(parentId);
            }
            adminMenu.setName(name);
            adminMenu.setUrl(url);
            adminMenuService.add(adminMenu);
            result.setCode(Result.Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }

    /**
     * 修改菜单信息
     * @param name 菜单名称
     * @param url   菜单跳转地址
     * @param id  菜单id
     * @return
     */
    @PostMapping("/edit")
    @ApiOperation("菜单：修改")
    public Result edit(String name, String url, Long id) {
        Result result = new Result();
        try {
            AdminMenu adminMenu = new AdminMenu();
            adminMenu.setId(id);
            adminMenu.setName(name);
            adminMenu.setUrl(url);
            adminMenu.setUpdatetime(new Date());
            adminMenuService.edit(adminMenu);
            result.setCode(Result.Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }

    /**
     * 获取菜单信息
     * @param id
     * @return
     */
    @PostMapping("/get")
    @ApiOperation("菜单：获取信息")
    public Result get(Long id) {
        Result result = new Result();
        try {
            result.setData(adminMenuService.get(id));
            result.setCode(Result.Code.SUCCESS);
        } catch (Exception e) {
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }

    /**
     * 删除菜单信息
     * @param id 菜单id
     * @return
     */
    @PostMapping("/delete")
    public Result delete(Long id) {
        Result result = new Result();
        try {
            adminMenuService.delete(id);
            result.setCode(Result.Code.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result.setCode(Result.Code.SYSTEMERROR);
            logger.error(e.getMessage());
        }
        return result;
    }

}
